var Config = require('./config');
var _language = window['_language'] = docCookies.getItem('_language') || 'zh';
$("html").addClass(_language);

$(document).on('click', function(e){
    if(!$(e.target).hasClass('checked')) $(".dropdown-list").slideUp('fast');
});
/* 避免avalon在domready时扫描body将script移出移入dom
* 导致script重新拉取的问题
* IE9及以下才有的问题https://github.com/RubyLouvre/avalon/issues/2040
*/
function avalonForIe7(avalon){
    avalon.config({
        debug: false,
        local: false
    })
    if (avalon.msie && avalon.msie < 10) {
        let childNodes = document.body.childNodes;
        for (let i = childNodes.length - 1, node; i >= 0; i--) {
            node = childNodes[i]
            if (node.nodeName === 'SCRIPT') {
                document.body.removeChild(node)
            }
        }
    }
}
/**
 * 重定向 location.href 地址， 自动检测简繁体页面
 * @param {String} _url  跳转地址
 */
function href(_url){
    var switchLanguage = _language==='zh' ? '':'_tw';
    var ext_reg = /(\.html)/;
    location.href= _url.replace(ext_reg, switchLanguage+'$1');
}
/**
 * jQuery.ajax 再封装
 * 主要是用于接口数据请求错误的拦截
 * @param {Object} _param  请求参数,包含请求地址、参数、类型等等
 * @param {Boolean} _param.alertMsg  返回的code不为0的时候，是否自动提msg信息
 */
function ajax(_param){
    if(!_param ) return false;
    // 默认配置
    var _default_param = {
        alertMsg: true,
        type:'post',
    };
    if( arguments.length===1 && typeof _param == 'string' ){
        _param = {
            url: _param,
            type:'post'
        }
    }
    var _paramsObj = $.extend({},_default_param, _param);
    _paramsObj.url = '/api'+_param.url+'.do';
    var ajax = $.ajax(_paramsObj).success(function(res){
        var code = res.code;
        if(_param.alertMsg && (!res || code!='0')){
            layer.alert(res.msg);
            return false;
        }
        // if(res.code==='0002'){
        //     /* 将res.code赋值为0,以免后面重复报错 */
        //     res.code=0;
        //     layer.alert('未登录或登录超时，请重新登录',function(){
        //         href('/login.html');
        //     });
        // }
    }).error(function(jqXHR, textStatus, errorThrown){
        console.error(_paramsObj.url,arguments);
        layer.alert('接口调用失败 <br>url: '+_param.url +' <br>textStatus: ' + textStatus);
    });
    return ajax;
}
/** 根据时区计算时间显示
 * @param {Number} _time 时间戳 如：1504245973370
 * @return {String} 转换后的时间 如：2017-9-1 14:08:54
 */
var offset = new Date().getTimezoneOffset() * 60000;
function timeZone(_time){
    var gmt_ = docCookies.getItem('timeZone') || Config.timeZone;
        gmt_ = Number(gmt_);
    var today = new Date(_time);
    var utc = today.getTime() + offset;
    var currentDate = new Date(utc + (3600000 * (gmt_)));
    var time = "";
    var year = currentDate.getFullYear();
    var month = currentDate.getMonth() + 1;
    var day = currentDate.getDate();
    var hour = currentDate.getHours();
    var minute = currentDate.getMinutes();
    var second = currentDate.getSeconds();
    if (hour < 10) {
        time += "0" + hour;
    } else {
        time += hour;
    }
    time += ":";
    if (minute < 10) {
        time += "0" + minute;
    } else {
        time += minute;
    }
    time += ":";
    if (second < 10) {
        time += "0" + second;
    } else {
        time += second;
    }
    return year + "-" + month + "-" + day + " "+time;
}

/* 退出 */
function logout(){
    var curPath = '#'+stateman.path;
    href('/login.html?url='+encodeURIComponent(curPath));
}
module.exports = {
    avalonForIe7,
    href,
    ajax,
    logout,
    timeZone,
    host:'http://172.30.6.37:8088/Frontoffice/',
    alert(s) {
        var arr = ['a', 'b'];
        var obj = {
            o: 1
        }
        // alert(JSON.stringify(obj));
    }
}